package com.example.myandroidtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;

public class MainActivity extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter adapter = new SimpleAdapter(this, GetSms(),
				R.layout.activity_main,
				new String[] { "Number", "Content", "CreateTime" }, new int[] {
						R.id.number, R.id.content, R.id.date });
		setListAdapter(adapter); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private List<Map<String, String>> GetSms() {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		String pathName = Environment.getExternalStorageDirectory().getPath();
		SQLiteDatabase sql = SQLiteDatabase.openOrCreateDatabase(pathName
				+ "/test.db", null);
		String sqlStr = getResources().getString(R.string.GetSms);
		Cursor isRead = sql.rawQuery(sqlStr, null);
		isRead.moveToFirst();
		while (!isRead.isAfterLast()) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("Number", isRead.getString(isRead.getColumnIndex("Number")));
			map.put("Content",
					isRead.getString(isRead.getColumnIndex("Content")));
			map.put("CreateTime",
					isRead.getString(isRead.getColumnIndex("CreateTime")));
			list.add(map);
			isRead.moveToNext();
		}
		return list;
	}
}
